<template>
  <div>
    <el-form :model="msgForm" label-width="100px">

      <el-form-item label="用户名">
        <el-input v-model="msgForm.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="msgForm.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="工号">
        <el-input v-model="msgForm.jobid" placeholder="请输入工号"></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="msgForm.rid" placeholder="请选择角色" style="width: 100%;">
          <el-option v-for="role in rolesMsg" :label="role.rolename" :value="role.id" :key="role.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="职务">
        <el-select v-model="msgForm.duty" placeholder="请选择职务" style="width: 100%;">
          <el-option v-for="zw in zwMsg" :label="zw.dicname" :value="zw.id" :key="zw.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="姓名">
        <el-input v-model="msgForm.name" placeholder="请输入姓名"></el-input>
      </el-form-item>

      <!-- :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload" -->
      <el-form-item label="头像" label-width="100px">
        <el-upload class="avatar-uploader" :on-success="handleAvatarSuccess" action="http://www.img.ttblog.cn/api/upload"
          :show-file-list="false">
          <!-- 图片懒加载 -->
          <!-- <img v-if="msgForm.img" v-lazy="`http://www.img.ttblog.cn/`+msgForm.img" class="avatar"/> -->
          <img v-if="msgForm.img" :src="`http://www.img.ttblog.cn/` + msgForm.img" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="性别">
        <el-select v-model="msgForm.sex" placeholder="请选择性别" style="width: 100%;">
          <el-option label="男" value="1"></el-option>
          <el-option label="女" value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="生日">
        <el-date-picker v-model="msgForm.birthday" value-format="yyyy-MM-dd" type="date" placeholder="选择日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="手机号">
        <el-input v-model="msgForm.phone" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="座机号">
        <el-input v-model="msgForm.tel" placeholder="请输入座机号"></el-input>
      </el-form-item>

      <el-form-item label="所属部门">
        <el-select v-model="msgForm.department" placeholder="请选择所属部门" style="width: 100%;">
          <el-option v-for="bm in bmMsg" :label="bm.dicname" :value="bm.id" :key="bm.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="负责部门">
        <el-select v-model="msgForm.resdepartment" placeholder="请选择负责部门" style="width: 100%;">
          <el-option v-for="bm in bmMsg" :label="bm.dicname" :value="bm.id" :key="bm.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="用户状态">
        <el-select v-model="msgForm.status" placeholder="请选择用户状态" style="width: 100%;">
          <el-option label="正常" :value="1"></el-option>
          <el-option label="冻结" :value="2"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

export default {
  name: 'AddUser',
  props: ['id', 'isEdit'],
  data() {
    return {
      zwMsg: [],
      rolesMsg: [],
      msgForm: {
        username: 'ttusername',
        password: 'ttttttt',
        jobid: '333025',
        rid: 1,
        duty: 26,
        name: '张三',
        img: '',
        sex: '男',
        birthday: '2023-11-03',
        phone: '18766668888',
        tel: '0354666',
        department: 30,
        resdepartment: 31,
        status: '1',
      },
    }
  },
  mounted() {
    this.getSystemZw();
    this.getSystemRoles();
    this.showForm()
    this.$bus.$on('addOrUpdateSystemUser', this.addOrUpdateSystemUser)
  },
  computed: {
    bmMsg() {
      return this.$parent.$parent.bmMsg;
    }
  },
  methods: {
    async getSystemZw() {
      let res = await this.$API.SystemMenus.reqSystemZw();
      this.zwMsg = res;
    },
    async getSystemRoles() {
      let res = await this.$API.SystemMenus.reqSystemRoles();
      if (res.code == 200) {
        this.rolesMsg = res.data;
      }
    },
    async addOrUpdateSystemUser() {
      let { username, password, jobid, rid, duty, name, img, sex, birthday, phone, tel, department, resdepartment, status } = this.msgForm;
      if (this.isEdit) {
        let {id} = this;
        let res = await this.$API.SystemMenus.reqUpdateSystemUser(id,username, password, jobid, rid, duty, name, img, sex, birthday, phone, tel, department, resdepartment, status);
        
        if (res.code == 1) {
          this.$message({ message: '编辑用户成功', type: 'success' })
        } else {
          this.$message({ message: '编辑失败', type: 'error' })
        }
      } else {
        let res = await this.$API.SystemMenus.reqAddSystemUser(username, password, jobid, rid, duty, name, img, sex, birthday, phone, tel, department, resdepartment, status);
        if (res.code == 1) {
          this.$message({ message: '添加用户成功', type: 'success' })
        } else {
          this.$message({ message: '添加失败', type: 'error' })
        }
      }
    },
    handleAvatarSuccess(res, file) {
      this.msgForm.img = res.data.slice(res.data.lastIndexOf('/') + 1, res.data.length);
    },
    async showForm() {
      if (this.isEdit) {
        let res = await this.$API.SystemMenus.reqSelectUserById(this.id);
        if (res.code == 200) {
          this.msgForm = { ...res.data };
          this.msgForm.duty = +this.msgForm.duty;
          this.msgForm.resdepartment = +this.msgForm.resdepartment;
          this.msgForm.department = +this.msgForm.department;
          this.msgForm.status = +this.msgForm.status;
        }
      } else {
        this.msgForm = {
          username: '',
          password: '',
          jobid: '',
          rid: '',
          duty: '',
          name: '',
          img: '',
          sex: '',
          birthday: '',
          phone: '',
          tel: '',
          department: '',
          resdepartment: '',
          status: '',
        };
      }
    }
  },
  watch: {
    'msgForm.birthday': {
      handler() {
        if (!this.msgForm.birthday) {
          this.msgForm.birthday = '';
        }
      }
    },
  },
  beforeDestroy() {
    this.$bus.$off('addOrUpdateSystemUser');
  },
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-upload--text {
  width: auto;
}
</style>